﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace PearTunes.Client
{
    public class PlayListManager
    {
        private List<PlayList> userPlaylists = new List<PlayList>();
        private PlayList localPlaylist = new PlayList(true);

        public int UserPlayListCount
        {
            get
            {
                return userPlaylists.Count;
            }
        }

        public PlayListManager()
        {
            LoadUserPlaylists();
        }
        ~PlayListManager()
        {
            SaveAllPlaylists();
        }

        public void Refresh()
        {
            localPlaylist = new PlayList(true);
        }

        private void LoadUserPlaylists()
        {
            List<string> dirs = FileHelper.GetFilesRecursive(Properties.Settings.Default.localPlaylistPath);

            foreach (string p in dirs)
            {
                if(Path.GetExtension(p) == ".xml")
                {
                    userPlaylists.Add(new PlayList(p));
                }
            }

        }
        public PlayList GetLocalPlayList()
        {
            return localPlaylist;
        }
        public PlayList GetUserPlayListAt(int at)
        {
            if (at <= userPlaylists.Count - 1)
                return userPlaylists[at];
            return null;
        }
        public string GetUserPlayListNameAt(int at)
        {
            if (at <= userPlaylists.Count - 1)
                return userPlaylists[at].Name;
            return null;
        }
        public PlayList GetUserPlayListByName(string name)
        {
            return userPlaylists.Where(x => x.Name == name).First();
        }
        public bool AddToUserPlayList(string name, LocalTrack trackToAdd)
        {
            if(userPlaylists.Where(x=> x.Name == name).Count() != 0){
                userPlaylists.Where(x => x.Name == name).First().Add(trackToAdd);
                SavePlaylist(name);
                return true;
            }
            else
            {
                PlayList toAdd = new PlayList(false) { Name = name };
                toAdd.Add(trackToAdd);
                userPlaylists.Add(toAdd);
                return true;
            }
        }
        public void DeleteUserPlaylist(string name)
        {
            string fileName = String.Format(@"{0}\{1}.xml", Properties.Settings.Default.localPlaylistPath, name);
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
                PlayList playlist = userPlaylists.Where((x => x.Name == name)).First();
                userPlaylists.Remove(playlist);
            }
        }
        public void SavePlaylist(string name)
        {
            if (!File.Exists(Properties.Settings.Default.localPlaylistPath)) return;
            string fileName = String.Format(@"{0}\{1}.xml", Properties.Settings.Default.localPlaylistPath, name);
            var playlist = userPlaylists.Where((x => x.Name == name)).First();

            XmlDocument xmlDoc = new XmlDocument();
            XmlNode xmlNode = xmlDoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
            xmlDoc.AppendChild(xmlNode);
            XmlElement xmlRootNode = xmlDoc.CreateElement("Tracks");

            //trackek hozzáadása
            foreach (LocalTrack track in playlist)
            {
                XmlElement xmlTrackNode = xmlDoc.CreateElement("Track");
                xmlTrackNode.AppendChild(xmlDoc.CreateTextNode(track.FilePath));
                xmlRootNode.AppendChild(xmlTrackNode);
            }
            xmlDoc.AppendChild(xmlRootNode);

            xmlDoc.Save(fileName);
        }
        public void SaveAllPlaylists()
        {
            foreach (PlayList p in userPlaylists)
            {
                SavePlaylist(p.Name);
            }
        }
    }
}
